package com.lln.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lln.pojo.DeviceBrand;
import com.lln.pojo.Result;
import com.lln.pojo.vo.PageVo;
import com.lln.service.DeviceBrandService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("device/brand")
public class DeviceBrandController {
    @Autowired
    private DeviceBrandService deviceBrandService;
    @Autowired
    private HttpServletResponse response;

    @GetMapping
    public Result findAll(Integer deviceTypeId, String keyword, PageVo pageVo) {
        Page<DeviceBrand> page = new Page<>(pageVo.getPageNum(), pageVo.getPageSize());
        QueryWrapper<DeviceBrand> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(keyword)) {
            queryWrapper.like("name", keyword);
            queryWrapper.or();
            queryWrapper.like("brand_code", keyword);
        }
        if (deviceTypeId != null) {
            queryWrapper.eq("device_type_id", deviceTypeId);
        }
        Page<DeviceBrand> deviceBrandPage = deviceBrandService.page(page, queryWrapper);
        if (deviceBrandPage.getRecords().isEmpty()) {
            return Result.failed("暂无数据");
        } else {
            return Result.success("查询的总记录数为：" + deviceBrandPage.getTotal(), deviceBrandPage.getRecords());
        }
    }

    @PostMapping
    public Result add(@RequestBody DeviceBrand deviceBrand) {
        deviceBrand.setCreateId(1);
        if (deviceBrandService.save(deviceBrand)) {
            return Result.success("添加成功", deviceBrand);
        } else {
            return Result.failed("添加失败");
        }
    }

    @PutMapping
    public Result update(@RequestBody DeviceBrand deviceBrand) {
        if (deviceBrandService.updateById(deviceBrand)) {
            return Result.success("修改成功", deviceBrand);
        } else {
            return Result.failed("修改失败");
        }
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        DeviceBrand deviceBrand = deviceBrandService.getById(id);
        if (deviceBrand == null) {
            return Result.failed("该设备品牌不存在");
        }
        return Result.success(deviceBrand);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        if (deviceBrandService.removeById(id)) {
            return Result.success("删除成功");
        } else {
            return Result.failed("删除失败");
        }
    }

    @DeleteMapping("more")
    public Result deleteMore(@RequestBody List<Integer> ids) {
        if (deviceBrandService.removeByIds(ids)) {
            return Result.success("批量删除成功");
        } else {
            return Result.failed("批量删除失败");
        }
    }

    @PostMapping("upload")
    public Result upload(@RequestParam(required = false) MultipartFile file) throws Exception {
        if (deviceBrandService.uploadDeviceBrand(file)) {
            return Result.success("上传成功");
        } else {
            return Result.failed("上传失败");
        }
    }

    @GetMapping("export")
    public void export() throws Exception {
        List<DeviceBrand> deviceBrands = deviceBrandService.list();
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("设备品牌表");
        Row rowTitle = sheet.createRow(0);
        Cell cell1 = rowTitle.createCell(0);
        cell1.setCellValue("id");
        Cell cell2 = rowTitle.createCell(1);
        cell2.setCellValue("品牌名称");
        Cell cell3 = rowTitle.createCell(2);
        cell3.setCellValue("设备型号");
        Cell cell4 = rowTitle.createCell(3);
        cell4.setCellValue("备注");
        Cell cell5 = rowTitle.createCell(4);
        cell5.setCellValue("设备类型");
        Cell cell6 = rowTitle.createCell(5);
        cell6.setCellValue("创建人");
        Cell cell7 = rowTitle.createCell(6);
        cell7.setCellValue("创建时间");
        sheet.setColumnWidth(2, 10 * 256);
        sheet.setColumnWidth(6, 20 * 256);
        for (int i = 0; i < deviceBrands.size(); i++) {
            Row row = sheet.createRow(i + 1);
            DeviceBrand deviceBrand = deviceBrands.get(i);
            Cell cell11 = row.createCell(0);
            cell11.setCellValue(deviceBrand.getId());
            Cell cell21 = row.createCell(1);
            cell21.setCellValue(deviceBrand.getName());
            Cell cell31 = row.createCell(2);
            cell31.setCellValue(deviceBrand.getBrandCode());
            Cell cell41 = row.createCell(3);
            cell41.setCellValue(deviceBrand.getRemark());
            Cell cell51 = row.createCell(4);
            cell51.setCellValue(deviceBrand.getDeviceTypeId());
            Cell cell61 = row.createCell(5);
            cell61.setCellValue(deviceBrand.getCreateId());
            Cell cell71 = row.createCell(6);
            cell71.setCellValue(deviceBrand.getCreateTime());
        }
        //获取当前日期
        String currentDate = new SimpleDateFormat("yyyy年MM月dd日").format(new Date());
        //设置下载的文件名
        String fileName = URLEncoder.encode(currentDate + "设备品牌表" + ".xlsx", "utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        workbook.write(response.getOutputStream());
    }
}
